package com.wen.controller;

import com.wen.pojo.Role;
import com.wen.pojo.User;
import com.wen.service.RoleService;
import com.wen.service.UserService;
import com.wen.utils.PageBean;
import com.wen.utils.Result;
import dto.AssignRoleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //分页查询用户列表
    @RequestMapping("/user-list")
    public Result<PageBean<User>> userList(Integer pageNum,Integer pageSize,User user){
        PageBean<User> allUserList = userService.allUserList(pageNum, pageSize, user);
        return Result.ok(200,"查询成功",allUserList);
    }

    //添加用户
    @RequestMapping("/addUser")
    public Result addUser(@RequestBody User user){
        Boolean b = userService.addUser(user);
        if (b){
            return Result.ok(200,"添加成功",null);
        }else{
            return Result.fail(1,"添加失败");
        }
    }
    //启用禁用用户
    @RequestMapping("/updateState")
    public Result updateState(@RequestBody User user){
        Result result = userService.setStateById(user);
        return result;
    }

    @Autowired
    private RoleService roleService;
    //查询用户以分配角色
    @RequestMapping("/user-role-list/{userId}")
    public Result userRoleList(@PathVariable Integer userId){
        List<Role> userList = roleService.findUserRoleById(userId);
        return Result.ok(200,"查询成功",userList);
    }

    //给用户分配角色
    @RequestMapping("/assignRole")
    public Result assignRole(@RequestBody AssignRoleDto assignRoleDto){
        return userService.assignRole(assignRoleDto);
    }

    //单个删除用户
    @RequestMapping("/deleteUser/{userId}")
    public Result deleteUser(@PathVariable Integer userId){
        int[] id = new int[] {userId};
        return userService.deleteUserList(id);
    }
    //批量删除用户
    @RequestMapping("/deleteUserList")
    public Result deleteUserList(@RequestBody int[] list){
        return userService.deleteUserList(list);
    }

    //修改用户信息
    @RequestMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        return userService.updUserInfo(user);
    }
    //重置密码
    @RequestMapping("/updatePwd/{userId}")
    public Result updatePwd(@PathVariable Integer userId){
        return userService.updatePwd(userId);
    }
}
